﻿using SqlSugar;
using SXICE.Core.Configs;
using SXICE.Core.Domains;
using SXICE.Core.Helpers;

namespace SXICE.Core
{
    /// <summary>
    /// 数据库上下文
    /// </summary>
    public class DBContext
    {
        public DBContext()
        {
            Db = new SqlSugarClient(new ConnectionConfig()
            {
#if DEBUG
                AopEvents = new AopEvents
                {
                    OnLogExecuted = (a, b) =>
                    {
                        try
                        {
                            foreach (var item in b)
                                switch (item.DbType)
                                {
                                    case System.Data.DbType.String:
                                    case System.Data.DbType.DateTime:
                                    case System.Data.DbType.DateTime2:
                                    case System.Data.DbType.Date:
                                    case System.Data.DbType.DateTimeOffset:
                                    case System.Data.DbType.Xml:
                                        a = a.Replace(item.ParameterName, $"'{item.Value.ToString()}'");
                                        break;
                                    default:
                                        a.Replace(item.ParameterName, item.Value.ToString());
                                        break;
                                }
                            RuntimeHelper.Resolve<Infrastructure.ILog>().Info(a);
                        }
                        catch { }
                    }
                },
#endif
                ConnectionString = RuntimeHelper.Resolve<DBConfigs>().SQLConnection,
                DbType = DbType.SqlServer,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute
            });
        }

        /// <summary>
        /// 用来处理事务多表查询和复杂的操作
        /// </summary>
        protected SqlSugarClient Db;

        /// <summary>
        /// 开始事务
        /// </summary>
        protected void BeginTran()
        {
            Db.BeginTran();
        }

        /// <summary>
        /// 提交事务
        /// </summary>
        protected void CommitTran()
        {
            Db.CommitTran();
        }

        /// <summary>
        /// 回滚事务
        /// </summary>
        protected void RollbackTran()
        {
            Db.RollbackTran();
        }
       
      
        /// <summary>
        /// 医生数据操作对象
        /// </summary>
        protected SimpleClient<Doctor> DoctorDB => new SimpleClient<Doctor>(Db);
       
        /// <summary>
        /// 补充体检库操作对象
        /// </summary>
        public SimpleClient<ReplenishPE> ReplenishPEDB => new SimpleClient<ReplenishPE>(Db);
        /// <summary>
        /// 菜单数据操作对象
        /// </summary>
        public SimpleClient<Menu> MenuDB => new SimpleClient<Menu>(Db);
        /// <summary>
        /// 角色数据操作对象
        /// </summary>
        public SimpleClient<Roles> RolesDB => new SimpleClient<Roles>(Db);
        /// <summary>
        /// 权限数据操作对象
        /// </summary>
        public SimpleClient<Rights> RightsDB => new SimpleClient<Rights>(Db);
    }
}